/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Message;
import Beans.User;
import Mail.Mail;
import dao.MessageDao;
import dao.UserDao;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**

 @author Marie-Claude
 */
@ManagedBean(name = "sendMessage")
@RequestScoped
public class SendMessage implements Serializable {

  private Message message;
  @ManagedProperty(value = "#{userProfileManager}")
  private UserProfileManager mbUserProfile;
  @ManagedProperty(value = "#{userManager}")
  private UserManager mbUser;
  @ManagedProperty(value = "#{userListManager}")
  private UserListManager mbUserList;
  private String sendToUsername;

  /**
   Creates a new instance of SendMessage
   */
  public SendMessage() {
  }

  public Message getMessage() {
    return message;
  }

  public void setMessage(Message message) {
    this.message = message;
  }

  public UserProfileManager getMbUserProfile() {
    return mbUserProfile;
  }

  public void setMbUserProfile(UserProfileManager mbUserProfile) {
    this.mbUserProfile = mbUserProfile;
  }

  public String getSendToUsername() {
    return sendToUsername;
  }

  public void setSendToUsername(String sendToUsername) {
    this.sendToUsername = sendToUsername;
  }

  public UserManager getMbUser() {
    return mbUser;
  }

  public void setMbUser(UserManager mbUser) {
    this.mbUser = mbUser;
  }

  public UserListManager getMbUserList() {
    return mbUserList;
  }

  public void setMbUserList(UserListManager mbUserList) {
    this.mbUserList = mbUserList;
  }

  @PostConstruct
  public void init() {
    message = new Message();
    message.setRecipientUsername(mbUserList.getSendToUsername());
    sendToUsername = mbUserProfile.getSendToUsername();
  }

  public String sendMessage(String username) {
    message.setSenderID(mbUser.getUser().getUserID());
    User u = UserDao.getByUsername(mbUserProfile.getSendToUsername());
    message.setRecipientID(u.getUserID());

    MessageDao.insertMessage(message);
    if (u.getIsWarnedByEmail()) {
      Mail.envoyerMail("notenuf007@hotmail.com", "Message reçu!", "Vous avez reçu un nouveau message de la part de " + mbUser.getUser().getUsername());
    }

    return "myMessages";
  }

}
